﻿using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Media;
using Foundation;

namespace Vent.ViewModels.Tools
{
    [DataContract]
    public abstract class Tool : ViewModel
    {
        protected Tool()
        {
            Initialize();
        }

        public TouchViewModel TouchViewModel { get; private set; }

        [DataMember]
        public string Name
        {
            get { return Get(() => Name, "Tool"); }
            set { Set(() => Name, value); }
        }

        [DataMember]
        public double Tickness
        {
            get { return Get(() => Tickness, 32.0); }
            set { Set(() => Tickness, value); }
        }

        [DataMember]
        public double Opacity
        {
            get { return Get(() => Opacity, 0.7); }
            set { Set(() => Opacity, value); }
        }

        [DataMember]
        public bool Water
        {
            get { return Get(() => Water); }
            set { Set(() => Water, value); }
        }

        public Brush Stroke
        {
            get { return Get(() => Stroke, new SolidColorBrush(Colors.Purple)); }
            set { Set(() => Stroke, value); }
        }

        [DataMember]
        public Color StrokeColor
        {
            get
            {
                var brush = Stroke as SolidColorBrush;
                return brush != null ? brush.Color : Colors.Purple;
            }
            set {Stroke = new SolidColorBrush(value);}
        }

        [OnDeserialized]
        public new void Initialize(StreamingContext context = default (StreamingContext))
        {
            TouchViewModel = Store.OfType<TouchViewModel>();
        }

        public virtual void AddPoint(Point point, bool start)
        {

        }

        public override string ToString()
        {
            return Name;
        }

        protected Brush GetAlignedBrush(Point point)
        {
            var brush = Water ? TouchViewModel.Background : Stroke;
            if (brush is SolidColorBrush) return brush;
            var imageBrush = (ImageBrush) TouchViewModel.Background;
            var alignedBrush = new ImageBrush
            {
                ImageSource = imageBrush.ImageSource,
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top,
                Transform = new TranslateTransform {X = -point.X + Tickness/2, Y = -point.Y + Tickness/2}
            };

            return alignedBrush;
        }
    }
}
